package com.next

class Company
{
    String serialNumber

    Industry industry
    //行业
    String name
    //公司
    String code
    //工商&机构代码

    String logo

    Boolean enableAccountTerritory = true
    Boolean enableContactTerritory = true
    Boolean inheritanceAccountTerritory = true
    Boolean enableOpportunityTerritory = true

    Boolean enablePayroll = false
    Boolean enableCustomizedOpptStage = false
    //如果为真，Opportunity.Stage 从 ProductOpportunityStage 取值
    Boolean enableOpportunityWorkflow = false
    //enableOpportunityWorkflow 开启的前提是 enableTerritory && !enableCustomizedOpptStage，如果选择 enableOpportunityWorkflow 为真，在 Opportunity 初始化的时候同时初始化工作流

    Integer maximumNumberOfAccount = 50
    //最大 Account 数量
    Integer maximumNumberOfUser = 50
    //最大 User 数量

    String fileServer

    User createBy
    User modifyBy

    Date createdDate = new Date()
    Date modifiedDate = new Date()

    static hasMany = [departments: Department, jobs: Job, positions: Position]

    static constraints = {
        industry nullable: true, blank: true
        name maxSize: 128, nullable: true, blank: true, unique: true
        code maxSize: 128, nullable: true, blank: true, unique: true
        logo nullable: true, blank: true, unique: true, maxSize: 256, url: true
        serialNumber maxSize: 128
        createdDate nullable: true, blank: true
        modifiedDate nullable: true, blank: true
        createBy nullable: true, blank: true
        modifyBy nullable: true, blank: true

        fileServer maxSize: 256, url: true, nullable: true, blank: true
    }

    static mapping = {
        cache true
    }

    String toString()
    {
        name
    }

    def springSecurityService

    def beforeInsert()
    {
        def username = springSecurityService?.getPrincipal()?.username
        createBy = User.findByUsername(username)
    }

    def beforeUpdate()
    {
        modifiedDate = new Date()
        def username = springSecurityService?.getPrincipal()?.username
        modifyBy = User.findByUsername(username)
    }
}
